package wine.logic.interfaces;

import java.util.HashMap;
import java.util.Map;

import javax.ejb.Remote;
import javax.ejb.Stateless;

import wine.ejb.utilities.BaseSessionBean;
import wine.ejb.utilities.PageInfo;
import wine.ejb.utilities.PagedResults;
import wine.ejb.utilities.Utility;
import wine.logic.bean.WInformationDao;
import wine.logic.entity.WInformation;
@Stateless
@Remote(WInformationDao.class)
public class WInformationImpl extends BaseSessionBean<WInformation> implements
		WInformationDao {

	@Override
	public PagedResults<WInformation> findInfoLst(WInformation info, PageInfo pageInfo) {
		StringBuffer ejbql = new StringBuffer("from WInformation where 1=1 ");
		Map<String, Object> params = new HashMap<String, Object>();
		if(Utility.isNotEmpty(info)){
			if(Utility.isNotEmpty(info.getSinfotitle())){
				params.put("title", info.getSinfotitle());
			}
		}
		return super.findListByEJBQL(ejbql.toString(), pageInfo, params);
	}
	public Integer findRecordByImoduleId(Long imoduleid) {
		Map<Object, Object> params = new HashMap<Object, Object>();
		StringBuffer ejbql = new StringBuffer(
				"select count(imoduleid) from WInformation where imoduleid=:imoduleid ");
		params.put("imoduleid", imoduleid);
		Object o = super.findSingleRecord(ejbql.toString(), params);
		return o == null ? 0 : Integer.parseInt(o.toString());
	}
	@Override
	public void deInfoByLotsIds(Long[] l) {
		for (int i = 0; i < l.length; i++) {
			delInfoPage(l[i]);
			delInfo(l[i]);
		}
	}
	
	public void delInfo(Long l) {
		StringBuffer sqlBuffer = new StringBuffer(
				"delete from JInformation where iinfoid=:l");
		em.createQuery(sqlBuffer.toString()).setParameter("l", l)
				.executeUpdate();
	}

	
	public void delInfoPage(Long l) {
		StringBuffer sqlBuffer = new StringBuffer(
				"delete from JInformationPage where iinfoid=:l");
		em.createQuery(sqlBuffer.toString()).setParameter("l", l)
				.executeUpdate();
	}
}
